﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ComponentsWizardPageView.xaml.cs" company="WildGums">
//   Copyright (c) 2008 - 2015 WildGums. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Orc.Wizard.Example.Wizard.Views
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Windows;
    using System.ServiceProcess;

    public partial class ComponentsWizardPageView 
    {
        public ComponentsWizardPageView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Process pro = new Process();
           // string batPath = @"D:\Services\Smop.Distribute.Broker\install.bat";
          //  string service = "SmopDistributeBroker";

            
          for(int i=0;i<2;i++)
            {
                ServicePath Service = new ServicePath();
                Service = GetService(i);
                if (!File.Exists(Service.batPath))
            {
                string a = string.Format("{0}文件不存在，请检查目录", Service.batPath);
                    continue;
                //MessageBox.Show(a);
            }
            FileInfo file = new FileInfo(Service.batPath);
            pro.StartInfo.WorkingDirectory = file.Directory.FullName;
            pro.StartInfo.FileName = Service.batPath;
            pro.StartInfo.CreateNoWindow = false;
            pro.Start();
            pro.WaitForExit();
                if (cheackservice(Service.service) == true)
                {
                    if (cheackstartserviece(Service.service))
                    {
                        //string a = string.Format("{0}服务已成功启动", Service.service);
                       // MessageBox.Show(a);
                    }
                }
            }
            MessageBox.Show("服务启动成功");
        }

        public class ServicePath
        {
            public string batPath { get; set; }
            public string service { get; set; }
        }

        public ServicePath GetService(int num)
        {
            ServicePath Service=new ServicePath();
            switch (num)
            {               
                case 0:
                    Service.batPath = @"D:\Services\Smop.Task.Schedule\install.bat";
                    Service.service = "SmopTaskSchedule";
                    return Service;

                case 1:
                    Service.batPath = @"D:\wwwroot\kaifa\server\dist\install.bat";
                    Service.service = "huagong";
                    return Service;
                default:
                    return null;
            }
           
        }
        static int nub = 0;

        public static bool cheackservice(string service)
        {
            bool restult = false;
            ServiceController[] services = ServiceController.GetServices();
            var ServiceName1 = "";

            foreach (ServiceController s in services)
            {
                // ServiceName1 = ServiceName1 +";"+ s.ServiceName.ToLower();
                if (s.ServiceName.ToLower() == service.ToLower())
                {
                    restult = true;
                }
            }
            //MessageBox.Show(ServiceName1+service.ToLower());
            return restult;
        }

        /// <summary>
        /// 检测服务是否启动
        /// </summary>
        /// <param name="servicename"></param>
        /// <returns></returns>
        public static bool cheackstartserviece(string servicename)
        {
            ServiceController psc = new ServiceController(servicename);
            bool bStartStatus = true;
            try
            {
                if (psc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    if (nub <= 5)
                    {
                        //如果服务停止了，就启动服务
                        nub++;
                       // startserviece(@"D:\Services\Smop.Distribute.Broker\start.bat");
                        cheackstartserviece(servicename);
                    }
                    else
                    {
                        bStartStatus = false;
                    }
                }

                return bStartStatus;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="batpath">为批处理文件路径</param>
        public static void startserviece(string batpath)
        {
            Process pro = new Process();
            if (!File.Exists(batpath))
            {
                string a = string.Format("{0}文件不存在，请检查目录", batpath);
                MessageBox.Show(a);
            }
            FileInfo file = new FileInfo(batpath);
            pro.StartInfo.WorkingDirectory = file.Directory.FullName;
            pro.StartInfo.FileName = batpath;
            pro.StartInfo.CreateNoWindow = false;
            pro.Start();
            pro.WaitForExit();
        }
    }
}